<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="链接 搭建vue3环境下载最新vue-cli 123npm install -g @vue/cli# ORyarn global add @vue/cli 使用vue --version检查版本大于@vue/cli 4.5.6即可。 vue-cli命令行搭建9步式对话。 1.使用vue create vue3-1创建2.选择自定义3.选择TypeScript4.创建完成后使用yarn serve运">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue3.0+TypeScript学习笔记">
<meta property="og:url" content="http://yoursite.com/2020/09/28/Vue3.0+TypeScript学习笔记/index.html">
<meta property="og:site_name" content="春风自是人间客">
<meta property="og:description" content="链接 搭建vue3环境下载最新vue-cli 123npm install -g @vue/cli# ORyarn global add @vue/cli 使用vue --version检查版本大于@vue/cli 4.5.6即可。 vue-cli命令行搭建9步式对话。 1.使用vue create vue3-1创建2.选择自定义3.选择TypeScript4.创建完成后使用yarn serve运">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-11-23T14:02:51.526Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue3.0+TypeScript学习笔记">
<meta name="twitter:description" content="链接 搭建vue3环境下载最新vue-cli 123npm install -g @vue/cli# ORyarn global add @vue/cli 使用vue --version检查版本大于@vue/cli 4.5.6即可。 vue-cli命令行搭建9步式对话。 1.使用vue create vue3-1创建2.选择自定义3.选择TypeScript4.创建完成后使用yarn serve运">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/09/28/Vue3.0+TypeScript学习笔记/">





  <title>Vue3.0+TypeScript学习笔记 | 春风自是人间客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">春风自是人间客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">是非成败转头空，青山依旧在，几度夕阳红。</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-program">
          <a href="/categories/program" rel="section">
            
            程序
          </a>
        </li>
      
        
        <li class="menu-item menu-item-reading">
          <a href="/categories/reading" rel="section">
            
            阅读
          </a>
        </li>
      
        
        <li class="menu-item menu-item-idea">
          <a href="/categories/idea" rel="section">
            
            想法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于我
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/09/28/Vue3.0+TypeScript学习笔记/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="彭丹丹">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="春风自是人间客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Vue3.0+TypeScript学习笔记</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-09-28T18:50:17+08:00">
                2020-09-28
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/program/" itemprop="url" rel="index">
                    <span itemprop="name">program</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,734
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          
          
        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><a href="https://jspang.com/detailed?id=64" target="_blank" rel="noopener">链接</a></p>
<h1 id="搭建vue3环境"><a href="#搭建vue3环境" class="headerlink" title="搭建vue3环境"></a>搭建vue3环境</h1><p>下载最新vue-cli</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm install -g @vue/cli</span><br><span class="line"># OR</span><br><span class="line">yarn global add @vue/cli</span><br></pre></td></tr></table></figure>
<p>使用<code>vue --version</code>检查版本大于<code>@vue/cli 4.5.6</code>即可。</p>
<h2 id="vue-cli命令行搭建"><a href="#vue-cli命令行搭建" class="headerlink" title="vue-cli命令行搭建"></a>vue-cli命令行搭建</h2><p>9步式对话。</p>
<p>1.使用<code>vue create vue3-1</code>创建<br>2.选择自定义<br>3.选择TypeScript<br>4.创建完成后使用<code>yarn serve</code>运行项目</p>
<h2 id="vue-cli图形界面搭建"><a href="#vue-cli图形界面搭建" class="headerlink" title="vue-cli图形界面搭建"></a>vue-cli图形界面搭建</h2><p>1.使用<code>vue ui</code>创建<br>2.打开运行的<code>http://localhost:80</code><br>3.创建新项目<br>4.创建完成后使用<code>yarn serve</code>运行项目</p>
<h1 id="setup和ref"><a href="#setup和ref" class="headerlink" title="setup和ref"></a>setup和ref</h1><p>做一个单行点击显示值事件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;div&gt;请选择&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;button</span><br><span class="line">      v-for=&quot;(item, index) in girls&quot;</span><br><span class="line">      v-bind:key=&quot;index&quot;</span><br><span class="line">      @click=&quot;selectGirlFun(index)&quot;</span><br><span class="line">    &gt;</span><br><span class="line">      &#123;&#123; index &#125;&#125; : &#123;&#123; item &#125;&#125;</span><br><span class="line">    &lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">  &lt;div&gt;你选择了【&#123;&#123; selectGirl &#125;&#125;】为你服务&lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123; defineComponent, ref &#125; from &quot;vue&quot;;</span><br><span class="line">export default defineComponent(&#123;</span><br><span class="line">  name: &quot;App&quot;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    const girls = ref([&quot;大脚&quot;, &quot;刘英&quot;, &quot;晓红&quot;]);</span><br><span class="line">    const selectGirl = ref(&quot;&quot;);</span><br><span class="line">    const selectGirlFun = (index: number) =&gt; &#123;</span><br><span class="line">      selectGirl.value = girls.value[index];</span><br><span class="line">    &#125;;</span><br><span class="line">    //因为在模板中这些变量和方法都需要条用，所以需要return出去。</span><br><span class="line">    return &#123;</span><br><span class="line">      girls,</span><br><span class="line">      selectGirl,</span><br><span class="line">      selectGirlFun,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<p><code>setup</code>函数代替了<code>vue2</code>中的<code>data</code>和<code>methods</code>属性</p>
<h1 id="优化程序"><a href="#优化程序" class="headerlink" title="优化程序"></a>优化程序</h1><h2 id="reactive优化"><a href="#reactive优化" class="headerlink" title="reactive优化"></a>reactive优化</h2><p>为什么要用reactive，因为不用的话，在方法里面要用<code>selectGirl.value</code>才能获取到值。</p>
<p>将所有对应的值封装为一个对象：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;button</span><br><span class="line">      v-for=&quot;(item, index) in data.girls&quot;</span><br><span class="line">      v-bind:key=&quot;index&quot;</span><br><span class="line">      @click=&quot;data.selectGirlFun(index)&quot;</span><br><span class="line">    &gt;</span><br><span class="line">      &#123;&#123; index &#125;&#125; : &#123;&#123; item &#125;&#125;</span><br><span class="line">&lt;/button&gt;</span><br><span class="line">&lt;div&gt;你选择了【&#123;&#123; data.selectGirl &#125;&#125;】为你服务&lt;/div&gt;</span><br><span class="line">...</span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123; ref, reactive &#125; from &quot;vue&quot;;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &quot;App&quot;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    const data = reactive(&#123;</span><br><span class="line">      girls: [&quot;大脚&quot;, &quot;刘英&quot;, &quot;晓红&quot;],</span><br><span class="line">      selectGirl: &quot;&quot;,</span><br><span class="line">      selectGirlFun: (index: number) =&gt; &#123;</span><br><span class="line">        data.selectGirl = data.girls[index];</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    return &#123;</span><br><span class="line">      data,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="给data增加类型注解优化"><a href="#给data增加类型注解优化" class="headerlink" title="给data增加类型注解优化"></a>给data增加类型注解优化</h2><p><code>data</code>变量没有作<code>类型注解</code>，而是采用了<code>TypeScript</code>的<code>类型推断</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">interface DataProps &#123;</span><br><span class="line">  girls: string[];</span><br><span class="line">  selectGirl: string;</span><br><span class="line">  selectGirlFun: (index: number) =&gt; void;</span><br><span class="line">&#125;</span><br><span class="line">const data: DataProps = ...</span><br></pre></td></tr></table></figure>
<h2 id="用toRefs优化DOM中的对象"><a href="#用toRefs优化DOM中的对象" class="headerlink" title="用toRefs优化DOM中的对象"></a>用toRefs优化DOM中的对象</h2><p>引入：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import &#123; reactive, toRefs &#125; from &quot;vue&quot;;</span><br></pre></td></tr></table></figure>
<p>JS:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">export default &#123;</span><br><span class="line">  name: &quot;App&quot;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    const data: DataProps = reactive(&#123;</span><br><span class="line">      girls: [&quot;大脚&quot;, &quot;刘英&quot;, &quot;晓红&quot;],</span><br><span class="line">      selectGirl: &quot;&quot;,</span><br><span class="line">      selectGirlFun: (index: number) =&gt; &#123;</span><br><span class="line">        data.selectGirl = data.girls[index];</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line">    const refData = toRefs(data);</span><br><span class="line"></span><br><span class="line">    return &#123;</span><br><span class="line">      ...refData,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>DOM:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;button</span><br><span class="line">      v-for=&quot;(item, index) in girls&quot;</span><br><span class="line">      v-bind:key=&quot;index&quot;</span><br><span class="line">      @click=&quot;selectGirlFun(index)&quot;</span><br><span class="line">    &gt;</span><br><span class="line">      &#123;&#123; index &#125;&#125; : &#123;&#123; item &#125;&#125;</span><br><span class="line">&lt;/button&gt;</span><br><span class="line">&lt;div&gt;你选择了【&#123;&#123; selectGirl &#125;&#125;】为你服务&lt;/div&gt;</span><br></pre></td></tr></table></figure>
<h1 id="生命周期"><a href="#生命周期" class="headerlink" title="生命周期"></a>生命周期</h1><h2 id="对比"><a href="#对比" class="headerlink" title="对比"></a>对比</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Vue2--------------vue3</span><br><span class="line">beforeCreate  -&gt; setup()</span><br><span class="line">created       -&gt; setup()</span><br><span class="line">beforeMount   -&gt; onBeforeMount</span><br><span class="line">mounted       -&gt; onMounted</span><br><span class="line">beforeUpdate  -&gt; onBeforeUpdate</span><br><span class="line">updated       -&gt; onUpdated</span><br><span class="line">beforeDestroy -&gt; onBeforeUnmount</span><br><span class="line">destroyed     -&gt; onUnmounted</span><br><span class="line">activated     -&gt; onActivated</span><br><span class="line">deactivated   -&gt; onDeactivated</span><br><span class="line">errorCaptured -&gt; onErrorCaptured</span><br></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    console.log(&quot;1-开始创建组件-----setup()&quot;);</span><br><span class="line">    const data: DataProps = reactive(&#123;</span><br><span class="line">      girls: [&quot;大脚&quot;, &quot;刘英&quot;, &quot;晓红&quot;],</span><br><span class="line">      selectGirl: &quot;&quot;,</span><br><span class="line">      selectGirlFun: (index: number) =&gt; &#123;</span><br><span class="line">        data.selectGirl = data.girls[index];</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line">    onBeforeMount(() =&gt; &#123;</span><br><span class="line">      console.log(&quot;2-组件挂载到页面之前执行-----onBeforeMount()&quot;);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    onMounted(() =&gt; &#123;</span><br><span class="line">      console.log(&quot;3-组件挂载到页面之后执行-----onMounted()&quot;);</span><br><span class="line">    &#125;);</span><br><span class="line">    onBeforeUpdate(() =&gt; &#123;</span><br><span class="line">      console.log(&quot;4-组件更新之前-----onBeforeUpdate()&quot;);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    onUpdated(() =&gt; &#123;</span><br><span class="line">      console.log(&quot;5-组件更新之后-----onUpdated()&quot;);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    const refData = toRefs(data);</span><br><span class="line"></span><br><span class="line">    return &#123;</span><br><span class="line">      ...refData,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h2 id="和vue2的生命周期也能混合使用"><a href="#和vue2的生命周期也能混合使用" class="headerlink" title="和vue2的生命周期也能混合使用"></a>和vue2的生命周期也能混合使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    ... //内容不变</span><br><span class="line">&#125;,</span><br><span class="line">beforeCreate() &#123;</span><br><span class="line">  console.log(&quot;1-组件创建之前-----beforeCreate()&quot;);</span><br><span class="line">&#125;,</span><br><span class="line">beforeMount() &#123;</span><br><span class="line">  console.log(&quot;2-组件挂载到页面之前执行-----BeforeMount()&quot;);</span><br><span class="line">&#125;,</span><br><span class="line">mounted() &#123;</span><br><span class="line">  console.log(&quot;3-组件挂载到页面之后执行-----Mounted()&quot;);</span><br><span class="line">&#125;,</span><br><span class="line">beforeUpdate() &#123;</span><br><span class="line">  console.log(&quot;4-组件更新之前-----BeforeUpdate()&quot;);</span><br><span class="line">&#125;,</span><br><span class="line">updated() &#123;</span><br><span class="line">  console.log(&quot;5-组件更新之后-----Updated()&quot;);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h1 id="onRenderTracked-和onRenderTriggered-钩子函数"><a href="#onRenderTracked-和onRenderTriggered-钩子函数" class="headerlink" title="onRenderTracked()和onRenderTriggered()钩子函数"></a>onRenderTracked()和onRenderTriggered()钩子函数</h1><h2 id="onRenderTracked状态跟踪"><a href="#onRenderTracked状态跟踪" class="headerlink" title="onRenderTracked状态跟踪"></a>onRenderTracked状态跟踪</h2><p>只要页面中有<code>update</code>的情况，就会跟踪生成<code>event</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">import &#123; .... ,onRenderTracked,&#125; from &quot;vue&quot;;</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">onRenderTracked((event) =&gt; &#123;</span><br><span class="line">  console.log(&quot;状态跟踪组件-----------&gt;&quot;);</span><br><span class="line">  console.log(event);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="onRenderTriggered状态触发"><a href="#onRenderTriggered状态触发" class="headerlink" title="onRenderTriggered状态触发"></a>onRenderTriggered状态触发</h2><p>只有发生变化的值才会触发</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">onRenderTriggered((event) =&gt; &#123;</span><br><span class="line">  console.log(&quot;状态触发组件---------------&gt;&quot;);</span><br><span class="line">  console.log(event);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>event:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- key 那边变量发生了变化</span><br><span class="line">- newValue 更新后变量的值</span><br><span class="line">- oldValue 更新前变量的值</span><br><span class="line">- target 目前页面中的响应变量和函数</span><br></pre></td></tr></table></figure>
<h1 id="watch"><a href="#watch" class="headerlink" title="watch"></a>watch</h1><p>例子上说<code>methods</code>无法改变<code>document.title</code>，要用<code>watch</code>监听后改，但我尝试后都是可以的。</p>
<p>方法1：methods:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">chooseFn: (index: number) =&gt; &#123;</span><br><span class="line">    data.currentValue = data.deviceList[index];</span><br><span class="line">    document.title = data.currentValue;</span><br><span class="line">    console.log(document.title)</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure></p>
<p>方法2：watch:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">watch(refData.currentValue, (newValue, oldValue) =&gt; &#123;</span><br><span class="line">    document.title = newValue;</span><br><span class="line">&#125;);</span><br><span class="line">return &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="模块化"><a href="#模块化" class="headerlink" title="模块化"></a>模块化</h1><h2 id="模块化JS功能"><a href="#模块化JS功能" class="headerlink" title="模块化JS功能"></a>模块化JS功能</h2><p>假设有很多页面需要公用一个时间函数，这时先新建一个<code>ts</code>文件，实现这个功能。</p>
<p>useNowTime.ts<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">import &#123;ref&#125; from &apos;vue&apos;;</span><br><span class="line"></span><br><span class="line">const nowTime = ref(&quot;00:00:00&quot;);</span><br><span class="line">const getNowTime = () =&gt; &#123;</span><br><span class="line">  const now = new Date();</span><br><span class="line">  const hour = now.getHours() &lt; 10 ? &quot;0&quot; + now.getHours() : now.getHours();</span><br><span class="line">  const minu =now.getMinutes() &lt; 10 ? &quot;0&quot; + now.getMinutes() : now.getMinutes();</span><br><span class="line">  const sec =now.getSeconds() &lt; 10 ? &quot;0&quot; + now.getSeconds() : now.getSeconds();</span><br><span class="line">  nowTime.value = hour + &quot;:&quot; + minu + &quot;:&quot; + sec;</span><br><span class="line">  setTimeout(getNowTime, 1000);   //每一秒执行一次这个方法</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">export &#123; nowTime, getNowTime&#125;</span><br></pre></td></tr></table></figure></p>
<p>引入：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div class=&quot;hello&quot;&gt;</span><br><span class="line">    &lt;h1&gt;&#123;&#123;nowTime&#125;&#125;&lt;/h1&gt;</span><br><span class="line">    &lt;div @click=&quot;getNowTime&quot;&gt;显示时间&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123; defineComponent &#125; from &apos;vue&apos;;</span><br><span class="line">import &#123;nowTime, getNowTime&#125; from &apos;../hooks/useNowTime&apos;;</span><br><span class="line">export default defineComponent(&#123;</span><br><span class="line">  name: &apos;HelloWorld&apos;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      nowTime,</span><br><span class="line">      getNowTime</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="模块化组件"><a href="#模块化组件" class="headerlink" title="模块化组件"></a>模块化组件</h2><p>随机图片展示：</p>
<p>useURLAxios.ts<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">import &#123;ref&#125; from &apos;vue&apos;;</span><br><span class="line">import axios from &apos;axios&apos;;</span><br><span class="line"></span><br><span class="line">function useUrlAxios(url: string) &#123;</span><br><span class="line">    const result = ref(null);</span><br><span class="line">    const loading = ref(true);</span><br><span class="line">    const loaded = ref(false);</span><br><span class="line">    const error = ref(null);</span><br><span class="line"></span><br><span class="line">    axios.get(url).then((res) =&gt; &#123;</span><br><span class="line">        loading.value = false;</span><br><span class="line">        loaded.value = true;</span><br><span class="line">        result.value = res.data;</span><br><span class="line">    &#125;).catch(e =&gt; &#123;</span><br><span class="line">        error.value = e;</span><br><span class="line">        loading.value = false;</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    return &#123;</span><br><span class="line">        result,</span><br><span class="line">        loading,</span><br><span class="line">        loaded,</span><br><span class="line">        error</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default useUrlAxios;</span><br></pre></td></tr></table></figure></p>
<p>使用：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div class=&quot;hello&quot;&gt;</span><br><span class="line">    &lt;div v-if=&quot;loading&quot;&gt;loading...&lt;/div&gt;</span><br><span class="line">    &lt;img v-if=&quot;loaded&quot; :src=&quot;result.message&quot; /&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123; defineComponent,ref &#125; from &apos;vue&apos;;</span><br><span class="line">import useUrlAxios from &apos;../hooks/useURLAxios&apos;;</span><br><span class="line">export default defineComponent(&#123;</span><br><span class="line">  name: &apos;HelloWorld&apos;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    const &#123;result, loading , loaded&#125; = useUrlAxios(&apos;https://dog.ceo/api/breeds/image/random&apos;)</span><br><span class="line">    return &#123;</span><br><span class="line">      result,</span><br><span class="line">      loading,</span><br><span class="line">      loaded</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h1 id="teleport"><a href="#teleport" class="headerlink" title="teleport"></a>teleport</h1><p>瞬间移动组件、独立组件。</p>
<h2 id="普通组件"><a href="#普通组件" class="headerlink" title="普通组件"></a>普通组件</h2><p>Modal.vue</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;modal&lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">export default &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<p>app.vue使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;modal /&gt;</span><br><span class="line">//...</span><br><span class="line">import modal from &quot;./components/Modal.vue&quot;;</span><br><span class="line">const app = &#123;</span><br><span class="line">  name: &quot;App&quot;,</span><br><span class="line">  components: &#123;</span><br><span class="line">    modal,</span><br><span class="line">  &#125;,</span><br><span class="line">  //...</span><br><span class="line">&#125;</span><br><span class="line">//...</span><br></pre></td></tr></table></figure>
<h2 id="使用teleport"><a href="#使用teleport" class="headerlink" title="使用teleport"></a>使用teleport</h2><p>更改modal.vue<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;teleport to=&quot;#modal&quot;&gt;</span><br><span class="line">    &lt;div&gt;modal&lt;/div&gt;</span><br><span class="line">    &lt;/teleport&gt;</span><br><span class="line">&lt;/template&gt;</span><br></pre></td></tr></table></figure></p>
<p>在html中使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</span><br><span class="line">  &lt;div id=&quot;modal&quot;&gt;&lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br></pre></td></tr></table></figure>
<p>如果在<code>app.vue</code>中没使用<code>&lt;modal /&gt;</code>组件的话，页面会保留<code>modal</code>的<code>dom</code>，但<code>modal</code>中的内容不会显示。</p>
<p>如果在<code>app.vue</code>中使用了<code>&lt;modal/ &gt;</code>组件 的话，页面会显<code>示modal</code>中的内容。</p>
<h1 id="Suspense异步请求组件"><a href="#Suspense异步请求组件" class="headerlink" title="Suspense异步请求组件"></a>Suspense异步请求组件</h1><h2 id="1-编写AsyncShow-vue异步返回数据的组件"><a href="#1-编写AsyncShow-vue异步返回数据的组件" class="headerlink" title="1. 编写AsyncShow.vue异步返回数据的组件"></a>1. 编写AsyncShow.vue异步返回数据的组件</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h1&gt;&#123;&#123;result&#125;&#125;&lt;/h1&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123; defineComponent &#125; from &quot;vue&quot;;</span><br><span class="line"></span><br><span class="line">export default defineComponent(&#123;</span><br><span class="line">    setup() &#123;</span><br><span class="line">        return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">            setTimeout(() =&gt; &#123;</span><br><span class="line">                return resolve(&#123;result: &apos;pengdan&apos;&#125;)</span><br><span class="line">            &#125;,2000);</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="2-引用"><a href="#2-引用" class="headerlink" title="2. 引用"></a>2. 引用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import AsyncShow from &quot;./components/AsyncShow.vue&quot;;</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">const app = &#123;</span><br><span class="line">  name: &quot;App&quot;,</span><br><span class="line">  components: &#123; AsyncShow &#125;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    return &#123;&#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="3-使用"><a href="#3-使用" class="headerlink" title="3. 使用"></a>3. 使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">  &lt;Suspense&gt;</span><br><span class="line">    &lt;template #default&gt;</span><br><span class="line">      &lt;AsyncShow /&gt;</span><br><span class="line">    &lt;/template&gt;</span><br><span class="line">    &lt;template #fallback&gt;</span><br><span class="line">      &lt;h1&gt;loading&lt;/h1&gt;</span><br><span class="line">    &lt;/template&gt;</span><br><span class="line">  &lt;/Suspense&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>
<p>这里页面上会先显示loading，2秒钟后显示返回的数据。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/09/18/JavaScript控制对象/" rel="next" title="JavaScript控制对象">
                <i class="fa fa-chevron-left"></i> JavaScript控制对象
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/10/10/《绝望的主妇》10月英语学习/" rel="prev" title="《绝望的主妇》10月英语学习">
                《绝望的主妇》10月英语学习 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODk2OC8xNTQ5Ng=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" alt="彭丹丹">
            
              <p class="site-author-name" itemprop="name">彭丹丹</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">183</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#搭建vue3环境"><span class="nav-number">1.</span> <span class="nav-text">搭建vue3环境</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#vue-cli命令行搭建"><span class="nav-number">1.1.</span> <span class="nav-text">vue-cli命令行搭建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue-cli图形界面搭建"><span class="nav-number">1.2.</span> <span class="nav-text">vue-cli图形界面搭建</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#setup和ref"><span class="nav-number">2.</span> <span class="nav-text">setup和ref</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#优化程序"><span class="nav-number">3.</span> <span class="nav-text">优化程序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#reactive优化"><span class="nav-number">3.1.</span> <span class="nav-text">reactive优化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#给data增加类型注解优化"><span class="nav-number">3.2.</span> <span class="nav-text">给data增加类型注解优化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#用toRefs优化DOM中的对象"><span class="nav-number">3.3.</span> <span class="nav-text">用toRefs优化DOM中的对象</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#生命周期"><span class="nav-number">4.</span> <span class="nav-text">生命周期</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#对比"><span class="nav-number">4.1.</span> <span class="nav-text">对比</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用"><span class="nav-number">4.2.</span> <span class="nav-text">使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#和vue2的生命周期也能混合使用"><span class="nav-number">4.3.</span> <span class="nav-text">和vue2的生命周期也能混合使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#onRenderTracked-和onRenderTriggered-钩子函数"><span class="nav-number">5.</span> <span class="nav-text">onRenderTracked()和onRenderTriggered()钩子函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#onRenderTracked状态跟踪"><span class="nav-number">5.1.</span> <span class="nav-text">onRenderTracked状态跟踪</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#onRenderTriggered状态触发"><span class="nav-number">5.2.</span> <span class="nav-text">onRenderTriggered状态触发</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#watch"><span class="nav-number">6.</span> <span class="nav-text">watch</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#模块化"><span class="nav-number">7.</span> <span class="nav-text">模块化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#模块化JS功能"><span class="nav-number">7.1.</span> <span class="nav-text">模块化JS功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模块化组件"><span class="nav-number">7.2.</span> <span class="nav-text">模块化组件</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#teleport"><span class="nav-number">8.</span> <span class="nav-text">teleport</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#普通组件"><span class="nav-number">8.1.</span> <span class="nav-text">普通组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用teleport"><span class="nav-number">8.2.</span> <span class="nav-text">使用teleport</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Suspense异步请求组件"><span class="nav-number">9.</span> <span class="nav-text">Suspense异步请求组件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-编写AsyncShow-vue异步返回数据的组件"><span class="nav-number">9.1.</span> <span class="nav-text">1. 编写AsyncShow.vue异步返回数据的组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-引用"><span class="nav-number">9.2.</span> <span class="nav-text">2. 引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-使用"><span class="nav-number">9.3.</span> <span class="nav-text">3. 使用</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">彭丹丹</span>

  
</div>


	<div class="powered-by">
	<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
	  本站访客数:<span id="busuanzi_value_site_uv"></span>
	</span>
	</div>
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  undefined
  
  <!-- 页面爆炸效果 -->
  
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true}});</script></body>
</html>
